/**
 * 
 */
package com.bolbachchan.blog.security;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;

/**
 * @author Chaitanya
 * 
 */
public final class BlogUser extends LdapUserDetailsImpl {

    private final static Logger LOG = Logger.getLogger(BlogUser.class);

    private static BlogUser user = null;

    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String phoneNbr;
    private String city;
    private String state;

    private Collection<GrantedAuthority> authorities;
    /**
     * 
     */
    private static final long serialVersionUID = -332915224388797074L;

    public BlogUser(String username, String password, boolean enabled, boolean accountNonExpired,
	    boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities) {
	super();
	this.authorities = authorities;
    }

    /**
     * Static method that returns a single instance of <code>BlogUser</code> for
     * the entire session of a logged in User.
     * 
     * @return single instance of <code>BlogUser</code>
     */
    public static BlogUser getInstance() {
	if (user == null) {
	    user = new BlogUser();
	    LOG.info("User object is created.");
	}
	return user;
    }

    /**
     * Default constructor is made private to as we only need a single instance
     * of this Object for the entire session. The first and only instance can be
     * created using static <code>getInstance</code> method.
     */
    private BlogUser() {
	super();
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
	return firstName;
    }

    /**
     * @param firstName
     *            the firstName to set
     */
    public void setFirstName(String firstName) {
	this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
	return lastName;
    }

    /**
     * @param lastName
     *            the lastName to set
     */
    public void setLastName(String lastName) {
	this.lastName = lastName;
    }

    /**
     * @return the username
     */
    public String getUsername() {
	return username;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(String username) {
	this.username = username;
    }

    /**
     * @return the email
     */
    public String getEmail() {
	return email;
    }

    /**
     * @param email
     *            the email to set
     */
    public void setEmail(String email) {
	this.email = email;
    }

    /**
     * @return the phoneNbr
     */
    public String getPhoneNbr() {
	return phoneNbr;
    }

    /**
     * @param phoneNbr
     *            the phoneNbr to set
     */
    public void setPhoneNbr(String phoneNbr) {
	this.phoneNbr = phoneNbr;
    }

    /**
     * @return the city
     */
    public String getCity() {
	return city;
    }

    /**
     * @param city
     *            the city to set
     */
    public void setCity(String city) {
	this.city = city;
    }

    /**
     * @return the state
     */
    public String getState() {
	return state;
    }

    /**
     * @param state
     *            the state to set
     */
    public void setState(String state) {
	this.state = state;
    }

    /**
     * @return the authorities
     */
    public Collection<GrantedAuthority> getAuthorities() {
	return authorities;
    }

    /**
     * @param authorities
     *            the authorities to set
     */
    public void setAuthorities(Collection<GrantedAuthority> authorities) {
	this.authorities = authorities;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = super.hashCode();
	result = prime * result + ((authorities == null) ? 0 : authorities.hashCode());
	result = prime * result + ((city == null) ? 0 : city.hashCode());
	result = prime * result + ((email == null) ? 0 : email.hashCode());
	result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
	result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
	result = prime * result + ((phoneNbr == null) ? 0 : phoneNbr.hashCode());
	result = prime * result + ((state == null) ? 0 : state.hashCode());
	return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (!super.equals(obj))
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	BlogUser other = (BlogUser) obj;
	if (authorities == null) {
	    if (other.authorities != null)
		return false;
	}
	else if (!authorities.equals(other.authorities))
	    return false;
	if (city == null) {
	    if (other.city != null)
		return false;
	}
	else if (!city.equals(other.city))
	    return false;
	if (email == null) {
	    if (other.email != null)
		return false;
	}
	else if (!email.equals(other.email))
	    return false;
	if (firstName == null) {
	    if (other.firstName != null)
		return false;
	}
	else if (!firstName.equals(other.firstName))
	    return false;
	if (lastName == null) {
	    if (other.lastName != null)
		return false;
	}
	else if (!lastName.equals(other.lastName))
	    return false;
	if (phoneNbr == null) {
	    if (other.phoneNbr != null)
		return false;
	}
	else if (!phoneNbr.equals(other.phoneNbr))
	    return false;
	if (state == null) {
	    if (other.state != null)
		return false;
	}
	else if (!state.equals(other.state))
	    return false;
	return true;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	return "BlogUser [firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", phoneNbr="
		+ phoneNbr + ", city=" + city + ", state=" + state + ", authorities=" + authorities + "]";
    }

}
